<?php
namespace app\index\controller;
use app\index\model\Systemt;
use app\index\model\Filex;
use app\index\model\Channel;
use think\Controller;
use think\Request;
use think\Config;
use think\Db;
// use app\index\
class Base extends Controller
{
    protected $meta;
    public function _initialize()
    {
        $systemtModel=new Systemt();
        $systems=$systemtModel->getSystem();

        $channelModel=new Channel();
        $channel=$channelModel->getChannel();
        $channel=$this->makeTree($channel);
        // print_r($channel);exit;
        // SEO 关键字
        $meta=$this->meta;
        $meta['title']=$systems['title'];
        $meta['keywords']=$systems['keywords'];
        $meta['descript']=$systems['descript'];

        $nowtime=date("Y-m-d H:i:s",time());

        // 菜单栏目
        // $channel=$this->get_channel();

        $fragcate=db("category")->where("status=1")->select();
        for ($i=0; $i < count($fragcate); $i++) {
            $childs=db("fragment")->where("status=1 and cid=".$fragcate[$i]['id'])->select(); 
            for ($j=0; $j < count($childs); $j++) { 
                if($childs[$j]['thumb']){
                    $childs[$j]['annexes']=db("filex")->where("id=".$childs[$j]['thumb'])->value("thumb");
                }
            }
            $fragments[$fragcate[$i]['id']]=array_column($childs,null,"id");
        }
        // print_r($fragments);exit;
        $this->assign("fragments",$fragments);        
        $this->assign("meta",$meta);
        $this->assign("pagebanner",$pagebanner);
        $this->assign("systems",$systems);
        $this->assign("channel",$channel);
        $this->assign("nowtime",$nowtime);
    }
    public function getIndexPage($cid){
        $tempid=db("channel")->where("id=".$cid)->value("template");        
        if($tempid){
            $viewPage=db("template")->where("id=".$tempid)->value("vpage");
        }else{
            $viewPage="index";
        }
        return $viewPage;
    }
    public function getDetailPage($cid){
        $viewPage="detail";
        $tempid=db("channel")->where("id=".$cid)->value("template");        
        if($tempid){
            $vpage=db("template")->where("id=".$tempid)->value("vpage");
            if($vpage && $vpage != 'index'){
                $viewPage=$vpage."_detail";
            }  
        }
        return $viewPage;
    }
    public function getData($mark,$where,$page){
        if($mark != 'index'){
            $getModel=new $mark();
        }
    }
    public function get_channel(){  
        // $where['type']=1;
        $where['status']=1;
        $modelx=db("modelx");
        $channel=db("channel")->where($where)->order("sort asc , id DESC")->select();
        
        for ($i=0; $i < count($channel); $i++) { 
            $channel[$i]['contro']=$modelx->where("id=".$channel[$i]['modelx'])->value("mark");
            $channel[$i]['level']=$modelx->where("id=".$channel[$i]['modelx'])->value("level");
            $channel[$i]['type']=$modelx->where("id=".$channel[$i]['modelx'])->value("type");
        }
        $channel=$this->makeTree($channel);
        // print_r($channel);exit;
        return $channel;
    }
    public function get_left_channel($pid){
        // $where['type']=1;
        $where['status']=1;
        $where['pid']=$pid;
        $modelx=db("modelx");
        $lchannel=db("channel")->where($where)->order("sort asc , id DESC")->select();
        
        for ($i=0; $i < count($lchannel); $i++) { 
            $lchannel[$i]['contro']=$modelx->where("id=".$lchannel[$i]['modelx'])->value("mark");
            // $lchannel[$i]['vpage']=db("template")->where("id=".$lchannel[$i]['template'])->value('vpage');
            // if(!$lchannel[$i]['vpage']){
            //     $lchannel[$i]['vpage']="index";
            // }
        }
        // $channel=$this->makeTree($channel);
        // print_r($channel);exit;
        return $lchannel;
        // $this->assign("channel",$channel);
    }
    public function get_banner($id){
        // $where['type']=1;
        $where['id']=$id;
        $thumb=db("channel")->where($where)->value("thumb");
        if($thumb){
            $banner=db("filex")->where("id=".$thumb)->value("thumb");
        }        
        return $banner;
        // $this->assign("channel",$channel);
    }
    
    public function get_product_cate($cid){
        $cate=db("channel")->where("pid=".$cid)->order("sort asc , id DESC")->select();
        for ($i=0; $i < count($cate); $i++) { 
            $cate[$i]['child']=db("channel")->where("pid=".$cate[$i]['id'])->select();
        }        
        
        return $cate;
    }
    public function get_table_name($cid){
        $moxing=db("menu")->where("id=".$cid)->value("moxing");
        // print_r($moxing);exit;
        $table_name=db("moxing")->where("id=".$moxing)->value("table_name");

        $table_pre=config('database.prefix');
        $prefix_length=strlen($table_pre);

        // $table_name=$this->get_mark_table($id);
        // print_r($prefix_length);exit;
        $table_name=substr($table_name,$prefix_length);
        
        return $table_name;
    }
    public function get_one_mess($id,$cid){
        $moxing=db("menu")->where("id=".$cid)->value("moxing");
        // print_r($moxing);exit;
        $table_name=db("moxing")->where("id=".$moxing)->value("table_name");

        $table_pre=config('database.prefix');
        $prefix_length=strlen($table_pre);

        // $table_name=$this->get_mark_table($id);
        // print_r($prefix_length);exit;
        $table_name=substr($table_name,$prefix_length);
        $where['id']=$id;
        $mess=db($table_name)->where($where)->find();
        return $mess;
    }
    public function get_mark($id){
        $mark=db("menu")->where("id=".$id)->value("mark");
        // print_r($moxing);exit;
        // $mxmark=db("moxing")->where("id=".$moxing)->value("mark");
        return $mark;
    }
    public function get_mess($id){
        $moxing=db("menu")->where("id=".$id)->value("moxing");
        // print_r($moxing);exit;
        $table_name=db("moxing")->where("id=".$moxing)->value("table_name");

        $table_pre=config('database.prefix');
        $prefix_length=strlen($table_pre);

        // $table_name=$this->get_mark_table($id);
        // print_r($prefix_length);exit;
        $table_name=substr($table_name,$prefix_length);
        $where['cid']=$id;
        $mess=db($table_name)->where($where)->select();
        return $mess;
    }
    public function upload_file(){
        // $file=$_FILES['file'];
        $file = request()->file('file');
        // $post=$_POST;
        // return json_encode($post);
        // print_r($file);exit();
        $mess=$this->upload($file);
        return json_encode($mess);
    }
    public function upload($file){
        // 获取表单上传文件 例如上传了001.jpg
        // $file = request()->file('image');
        
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            // $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                // echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $filename=$info->getSaveName();
                $mess['code']=1;
                $mess['msg']="上传成功";
                $mess['filename']=$filename;
                return $mess;
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                // return $info->getFilename(); 
            }else{
                // 上传失败获取错误信息
                $error=$file->getError();
                $mess['code']=0;
                $mess['msg']=$error;
                $mess['error']=$error;
                return $mess;
            }
        }
    }
    public  function  getMenu($data,$pid=0,$deep=0)
    {
        $tree=array();
        foreach ($data as $row) {
            if($row['pid']==$pid){
                $row['child']=$this->getMenu($data,$row['id'],$deep+1);
                $tree[]=$row;
            }
        }
        return $tree;
    }
    // 按照顺序构造一级数组，根据level判断层级
    public function getTree($array, $pid =0, $level = 0){

        //声明静态数组,避免递归调用时,多次声明导致数组覆盖
        static $list = [];
        foreach ($array as $key => $value){
            //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['pid'] == $pid){
                //父节点为根节点的节点,级别为0，也就是第一级
                $value['level'] = $level;
                //把数组放到list中
                $list[] = $value;
                //把这个节点从数组中移除,减少后续递归消耗
                unset($array[$key]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree($array, $value['id'], $level+1);

            }
        }
        return $list;
    }
    function makeTree($list, $pk='id', $pid = 'pid', $child = 'child', $root = 0) {
      // 创建Tree
      $tree = array();
      if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
          $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
          // 判断是否存在parent
          $parentId = $data[$pid];
          if ($root == $parentId) {
            $tree[] =& $list[$key];
          }else{
            if (isset($refer[$parentId])) {
              $parent =& $refer[$parentId];
              $parent[$child][] =& $list[$key];
            }
          }
        }
      }
      return $tree;
    }
}
